package org.lc.solon.cmd.impl;

import org.jline.console.impl.JlineCommandRegistry;
import org.lc.solon.cmd.Command;
import org.lc.solon.cmd.SolonCommandRegistry;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class DefaultSolonCommandRegistry extends JlineCommandRegistry implements SolonCommandRegistry {
    private static volatile DefaultSolonCommandRegistry INSTANCE;

    public static DefaultSolonCommandRegistry getInstance() {
        if (INSTANCE == null) {
            synchronized (DefaultSolonCommandRegistry.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DefaultSolonCommandRegistry();
                }
            }
        }
        return INSTANCE;
    }

    private DefaultSolonCommandRegistry() {
    }

    @Override
    public List<String> commandInfo(String command) {
        return Collections.emptyList();
    }

    private Map<String, Command> commands;

    public Map<String, Command> getCommands() {
        return commands;
    }

    /**
     * 注册命令
     *
     * @param commands 命令
     */
    @Override
    public void registerCommands(List<Command> commands) {
        this.commands = commands.stream().collect(Collectors.toMap(Command::getName, Function.identity()));
        registerCommands(commands.stream().collect(Collectors.toMap(Command::getName, Command::toCommandMethods)));
    }
}
